package com.talkweb.ncfw.security.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.talkweb.ncfw.cache.ICacheManager;

/**
 * <p>文件名称: CacheFuncPermissionStore.java</p>
 * <p>文件描述: 本类描述</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-10-22</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class CacheFuncPermissionStore implements IFuncPermissionStore {
	private static Log log = LogFactory.getLog(CacheFuncPermissionStore.class);
	/** 功能许可cache键*/
	private static final String FUNC_PERMISSIONS_KEY = "FUNC_PERMISSIONS_KEY";
	private ICacheManager cacheManager;	
	
	public ICacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(ICacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-23
	 * @Description：获取功能许可信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Map<String, FuncPermission> getFuncPermissionData () {
		Object funcPermissionData = null;
		try {
			funcPermissionData = this.cacheManager.getElement(FUNC_PERMISSIONS_KEY);
		} catch (Exception e) {
			log.error("获取功能许可信息时发生异常.", e);
		}
		return (funcPermissionData == null) ? null : (Map<String, FuncPermission>)funcPermissionData;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-23
	 * @Description：保存功能许可信息
	 * @param funcPermissions
	 */
	private void saveFuncPermissionData (Map<String, FuncPermission> funcPermissionData) {
		try {
			this.cacheManager.putElement(FUNC_PERMISSIONS_KEY, funcPermissionData);
		} catch (Exception e) {
			log.error("保存功能许可信息时发生异常.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FuncPermission> listFuncPermissions() {
		Map<String, FuncPermission> funcPermissionData = this.getFuncPermissionData();
		if (funcPermissionData == null || funcPermissionData.isEmpty()) {
			return null;
		}
		Iterator iter = funcPermissionData.entrySet().iterator();
		List<FuncPermission> funcPermissionList = new ArrayList<FuncPermission>(funcPermissionData.keySet().size());
		while (iter.hasNext()) {
			Map.Entry entry 	= (Map.Entry) iter.next();
			funcPermissionList.add((FuncPermission)entry.getValue());
		}
		return funcPermissionList;
	}

	public FuncPermission getFuncPermission(String funccode) {
		Map<String, FuncPermission> funcPermissionData = this.getFuncPermissionData();
		if (funcPermissionData == null || funcPermissionData.isEmpty()) {
			return null;
		}
		return funcPermissionData.get(funccode);
	}

	public void removeAll() {
		try {
			this.cacheManager.removeElement(FUNC_PERMISSIONS_KEY);
		} catch (Exception e) {
			log.error("移除所有功能许可信息时发生异常.", e);
		}
	}

	public void removeFuncPermissions(String... funccodes) {
		Map<String, FuncPermission> funcPermissions = this.getFuncPermissionData();
		if (funcPermissions == null || funcPermissions.isEmpty()) {
			return;
		}
		if (funccodes == null || funccodes.length == 0) {
			return;
		}
		for (String funccode : funccodes) {
			if (funcPermissions.containsKey(funccode)) {
				funcPermissions.remove(funccode);
			}
		}
	}

	public void saveFuncPermissions(FuncPermission... funcPermissions) {
		Map<String, FuncPermission> funcPermissionData = this.getFuncPermissionData();
		if (funcPermissionData == null) {
			funcPermissionData = new HashMap<String, FuncPermission>();
		}
		for (FuncPermission funcPermission : funcPermissions) {
			if (!funcPermissionData.containsKey(funcPermission.getFunccode())) {
				funcPermissionData.put(funcPermission.getFunccode(), funcPermission);
			} 
		}
		this.saveFuncPermissionData(funcPermissionData);
	}

}

